package com.treasure.mall.biz.repository.contact;

import com.treasure.mall.biz.base.JpaSearchableRepository;
import com.treasure.mall.biz.entity.contact.ChatPO;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.repository.contact
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/8/11 5:29 下午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 *
 */
@Repository
public interface ChatRepository extends JpaSearchableRepository<ChatPO, Long> {

    /**
     * 根据用户id和商家ID获取群组数量
     *
     * @param userId
     * @param merchantId
     * @return
     */
    long countByUserPO_UserIdAndMerchantIdAndDeletedIsFalse(Long userId, Long merchantId);

    /**
     * 获取未删除的分组聊天记录
     *
     * @param merchantId
     * @return
     */
    List<ChatPO> findByMerchantIdAndDeletedIsFalse(Long merchantId);


    /**
     * 根据用户ID，获取未删除的分组聊天记录
     *
     * @param userId
     * @return
     */
    List<ChatPO> findByUserPO_UserIdAndDeletedIsFalse(Long userId);

    /**
     * 根据用户ID，获取未删除的分组聊天记录 根据回复时间倒序
     *
     * @param userId
     * @return
     */
    List<ChatPO> findByUserPO_UserIdAndDeletedIsFalseOrderByLastReplyTimeDesc(Long userId);


    /**
     * 修改聊天最后回复消息
     *
     * @param userId        用户ID
     * @param merchantId    商户ID
     * @param message       回复内容
     * @param lastReplyTime 最后回复时间
     */
    @Query("update ChatPO chat set chat.noReadCount=chat.noReadCount+1,chat.lastReplyMessage =?3,chat.lastReplyTime=?4 where chat.userPO.userId=?1 and chat.merchantId=?2")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void modifyLastMessage(Long userId, Long merchantId, String message, LocalDateTime lastReplyTime);


    @Query("update ChatPO chat set chat.userNoReadCount=chat.userNoReadCount+1,chat.lastReplyMessage =?3,chat.lastReplyTime=?4 where chat.userPO.userId=?1 and chat.merchantId=?2")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void modifyLastMessageByMerchantId(Long userId, Long merchantId, String message, LocalDateTime lastReplyTime);

    /**
     * 重置聊天未读消息
     *
     * @param userId
     * @param merchantId
     */
    @Query("update ChatPO chat set chat.noReadCount=0 where chat.userPO.userId=?1 and chat.merchantId=?2")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void resetNoReadCount(Long userId, Long merchantId);


    /**
     * 重置用户回话聊天未读消息
     *
     * @param userId
     * @param merchantId
     */
    @Query("update ChatPO chat set chat.userNoReadCount=0 where chat.userPO.userId=?1 and chat.merchantId=?2")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void resetUserNoReadCount(Long userId, Long merchantId);


    /**
     * 激活回话聊天
     *
     * @param userId
     * @param merchantId
     */
    @Query("update ChatPO chat set chat.deleted=false where chat.userPO.userId=?1 and chat.merchantId=?2")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void enableChat(Long userId, Long merchantId);

    /**
     * 删除回话聊天
     *
     * @param id
     */
    @Query("update ChatPO chat set chat.deleted=true where chat.id=?1")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void deleteChat(Long id);


}
